// miniprogram/pages/study/study.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    btnText: '下一题',
    status: 0,
    idx: 0,
    length: 0,
    question: {

    },
    questions: [],
    selectedOption: {
      code: '',
      content: '',
      value: -1
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    let uuid = options.uuid;
    this.setData({
      uuid
    },()=>{
      this.onQuery();
    })
    
  },
  onQuery: function(){
    let that = this;
    const db = wx.cloud.database();

    db.collection('notes').where({
      _openid: app.globalData.openid,
      uuid: this.data.uuid
    })
    .get()
    .then(res => {
      console.log('[数据库] [查询记录] 成功: ', res)
      let items = res.data;
      let questions = [];
      items.map((it)=>{
        questions.push(it.question);
      })
      // questions.map((qu)=>{
      //   qu.options = qu.options;
      // })
      let question = {};
      let options = [];
      if(items.length > 0){
        question = questions[0];
        options = question.options;
      }
      that.setData({
        question,
        options,
        questions: questions,
        length: questions.length
      })
      wx.setStorageSync('questions', items)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  doNext: function(){
    let idx = this.data.idx+1;
    let length = this.data.length;
    if(length-idx == 1){
      this.setData({
        btnText: '返回首页'
      })
    }
    if(idx == length){
      wx.navigateTo({
        url: '../home/home'
      })
      return;

    }
    let question = this.data.questions[idx];
    let options = question.options;
    this.setData({
      status: 0,
      idx,
      options,
      question
    })
  }
})